/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.InfoSemesterYearDTO;
import java.sql.ResultSet;

/**
 *
 * @author 11520_000
 */
public class InfoSemesterYearDAL {
    
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public InfoSemesterYearDAL(){
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    
    public ResultSet checkInfoSemesterYearIsHaveInDatabase(String _idSemester, String _idYear)
    {
        try
        {
            m_sqlCmd = "{Call CheckInfoSemesterYearIsHaveInDatabase(?,?)}";
            String[] arrId = {_idSemester, _idYear };
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,arrId);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
     /**
     * Ham lay tat ca cac khoa
     *
     * @return ResultSet
     */
    public ResultSet getAllInfoSemesterYear(){
        try
        {
            m_sqlCmd = "{Call GetAllInfoSemesterYearFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getInfoSemesterYearById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetInfoSemesterYearByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getInfoSemesterYearByIdSemesterAndIdYear(String _idSemester, String _idYear)
    {
        try {
            m_sqlCmd = "{Call GetInfoSemesterYearByIdSemesterAndIdYearFromDatabase(?,?)}";
            String[] arrId = {_idSemester, _idYear};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getInfoSemesterYearByIdYear(String _idYear)
    {
        try {
            m_sqlCmd = "{Call GetInfoSemesterYearByIdYearFromDatabase(?)}";
            String[] arrId = {_idYear};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
     
    /**
     * ham them moi khoa xuong database
     * @param _infoSemesterYear
     * @return true: success, false: fail
     */
    public boolean insertInfoSemesterYearToDatabase(InfoSemesterYearDTO _infoSemesterYear)
    {
       try {
           m_sqlCmd = "{Call InsertInfoSemesterYearToDatabase(?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_infoSemesterYear.getInfoSemesterYearID(), 
                _infoSemesterYear.getSemesterID(), 
                _infoSemesterYear.getAcademicYearID(), 
                _infoSemesterYear.getDateStartCourseRegistration(), 
                _infoSemesterYear.getDealineCourseRegistration(), 
                _infoSemesterYear.getDealineFee(), 
                _infoSemesterYear.getDealinePromation(), 
                _infoSemesterYear.getRatePromotion()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _infoSemesterYear
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateInfoSemesterYearToDatabase(InfoSemesterYearDTO _infoSemesterYear)
    {
       try {
           m_sqlCmd = "{Call UpdateInfoSemesterYearToDatabase(?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_infoSemesterYear.getInfoSemesterYearID(), 
                _infoSemesterYear.getSemesterID(), 
                _infoSemesterYear.getAcademicYearID(), 
                _infoSemesterYear.getDateStartCourseRegistration(), 
                _infoSemesterYear.getDealineCourseRegistration(), 
                _infoSemesterYear.getDealineFee(), 
                _infoSemesterYear.getDealinePromation(), 
                _infoSemesterYear.getRatePromotion()};
           
           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _infoSemesterYear
     * @return 
     */
     public boolean deleteInfoSemesterYearFromDatabase(String _infoSemesterYearID)
    {
        try {
           m_sqlCmd = "{Call DeleteInfoSemesterYearFromDatabase(?)}";
           String[] arrStr = {_infoSemesterYearID};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
